// 分类数据
import { topCategory } from '@/api/constants'
import { findAllCategory } from '@/api/category.js'
export default {
  namespaced: true,
  state () {
    return {
      // 赋值一个新数组
      list: topCategory.map(item => ({ name: item }))
    }
  },
  mutations: {
    setList (state, info) {
      state.list = info
    },
    // 定义修改显示与隐藏的函数,当前分类要用find查找对应的数据传入id
    show (state, info) {
      const category = state.list.find(el => el.id === info.id)
      category.open = true
    },
    hide (state, info) {
      const category = state.list.find(el => el.id === info.id)
      category.open = false
    }
  },
  actions: {
    // 获取分类数据
    async getList ({ commit }) {
      const data = await findAllCategory()
      data.result.forEach(el => {
        el.open = false
      })
      commit('setList', data.result)
    }
  }
}
